<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/** @var $this \Magento\Reports\Block\Adminhtml\Grid */
$numColumns = sizeof($this->getColumns());
?>
<?php if ($this->getCollection()): ?>
<?php if ($this->canDisplayContainer()): ?>
    <div id="<?php echo $this->getId() ?>">
<?php else: ?>
    <?php echo $this->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
<?php endif; ?>
<?php if ($this->getStoreSwitcherVisibility() || $this->getDateFilterVisibility()): ?>
    <div class="grid-actions">
        <?php if ($this->getChildBlock('grid.export')): ?>
        <div class="export">
            <?php echo $this->getChildHtml('grid.export');?>
        </div>
        <?php endif; ?>
        <?php if ($this->getDateFilterVisibility()): ?>
        <div class="filter required" id="<?php echo $this->getSuffixId('period_date_range')?>">
            <div class="field date">
                <label class="label">
                    <span><?php echo __('From') ?>:</span>
                    <input class="input-text no-changes required-entry"
                           type="text"
                           id="<?php echo $this->getSuffixId('period_date_from') ?>"
                           name="report_from"
                           value="<?php echo $this->getFilter('report_from') ?>">
                    <div id="<?php echo $this->getSuffixId('period_date_from_advice')?>"></div>
                </label>
            </div>

            <div class="field date">
                <label class="label">
                    <span><?php echo __('To') ?>:</span>
                    <input class="input-text no-changes required-entry"
                           type="text"
                           id="<?php echo $this->getSuffixId('period_date_to') ?>"
                           name="report_to"
                           value="<?php echo $this->getFilter('report_to') ?>"/>
                    <div id="<?php echo $this->getSuffixId('period_date_to_advice')?>"></div>
                </label>
            </div>

            <div class="show-by">
                <label class="label">
                    <span><?php echo __('Show By') ?>:</span>
                    <select name="report_period" id="<?php echo $this->getSuffixId('report_period')?>" class="select">
                        <?php foreach ($this->getPeriods() as $_value => $_label): ?>
                        <option value="<?php echo $_value ?>" <?php if ($this->getFilter('report_period') == $_value): ?> selected<?php endif; ?>><?php echo $_label ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <?php echo $this->getRefreshButtonHtml() ?>
            </div>
            <script type="text/javascript">
require([
    "jquery",
    "mage/calendar"
], function($){

    $("#<?php echo $this->getSuffixId('period_date_range') ?>").dateRange({
        dateFormat:"<?php echo $this->getDateFormat() ?>",
        buttonImage:"<?php echo $this->getViewFileUrl('images/grid-cal.gif') ?>",
        buttonText:"<?php echo __('Select Date') ?>",
        from:{
            id:"<?php echo $this->getSuffixId('period_date_from')?>"
        },
        to:{
            id:"<?php echo $this->getSuffixId('period_date_to')?>"
        }
    });           
});
</script>
        </div>
        <?php endif; ?>
    </div>
<?php endif; ?>
    <div class="grid">
        <div class="hor-scroll">
            <table cellspacing="0" class="data" id="<?php echo $this->getId() ?>_table">
                <!-- Rendering column set -->
                <?php echo $this->getChildHtml('grid.columnSet'); ?>
            </table>
        </div>
    </div>
</div>
<?php if ($this->canDisplayContainer()): ?>
    <script type="text/javascript">
require([
    "jquery",
    "mage/mage",
    "mage/adminhtml/grid"
], function(jQuery){

        jQuery.mage.load('validation');
        //<![CDATA[
            <?php echo $this->getJsObjectName() ?> = new varienGrid('<?php echo $this->getId() ?>', '<?php echo $this->getGridUrl() ?>', '<?php echo $this->getVarNamePage() ?>', '<?php echo $this->getVarNameSort() ?>', '<?php echo $this->getVarNameDir() ?>', '<?php echo $this->getVarNameFilter() ?>');
            <?php echo $this->getJsObjectName() ?>.useAjax = '<?php if ($this->getUseAjax()): echo $this->getUseAjax(); endif; ?>';
            <?php if ($this->getDateFilterVisibility()): ?>
            <?php echo $this->getJsObjectName() ?>.doFilterCallback = validateFilterDate;
        var period_date_from = $('<?php echo $this->getSuffixId('period_date_from')?>');
        var period_date_to = $('<?php echo $this->getSuffixId('period_date_to')?>');
        period_date_from.adviceContainer = $('<?php echo $this->getSuffixId('period_date_from_advice')?>');
        period_date_to.adviceContainer = $('<?php echo $this->getSuffixId('period_date_to_advice')?>');

        validateFilterDate = function() {
            if (period_date_from && period_date_to) {
                var valid = true;
                jQuery(period_date_from).add(period_date_to).each(function() {
                    valid = Validation.validate(this) && valid;
                });
                return valid;
            }
            else {
                return true;
            }
        }
            <?php endif;?>
            <?php if ($this->getStoreSwitcherVisibility()): ?>
        /* Overwrite function from switcher.phtml widget*/
        switchStore = function(obj) {
            if (obj.options[obj.selectedIndex].getAttribute('website') == 'true') {
                var selectionType = 'website';
            } else if (obj.options[obj.selectedIndex].getAttribute('group') == 'true') {
                var selectionType = 'group';
            } else {
                var selectionType = 'store';
            }
            var storeParam = obj.value ? selectionType + '/' + obj.value + '/' : '';
            if (obj.switchParams) {
                storeParam += obj.switchParams;
            }
            var formParam = new Array('<?php echo $this->getSuffixId('period_date_from')?>', '<?php echo $this->getSuffixId('period_date_to')?>', '<?php echo $this->getSuffixId('report_period')?>');
            var paramURL = '';
            var switchURL = '<?php echo $this->getAbsoluteGridUrl(array('_current' => false)); ?>'.replace(/(store|group|website)\/\d+\//, '');

            for (var i = 0; i < formParam.length; i++) {
                if ($(formParam[i]).value && $(formParam[i]).name) {
                    paramURL += $(formParam[i]).name + '=' + escape($(formParam[i]).value) + '&';
                }
            }
            setLocation(switchURL + storeParam + '?' + paramURL);
        }
            <?php endif; ?>
        //]]>
    
});
</script>
    <?php endif; ?>
<?php endif; ?>
